"""PAMR Predict Many

.. helpdoc::
Predict Classes for many observations given a PAMR trained model and data.

"""


"""<widgetXML>
    <name>PAMR Predict Many</name>
    <icon></icon>
    <tags>
        <tag>PAMR</tag>
    </tags>
    <summary>Predict Classes for many observations given a PAMR trained model and data.</summary>
    <author>
        <authorname>Red-R Core Development Team</authorname>
        <authorcontact>www.red-r.org</authorcontact>
    </author>
    </widgetXML>
"""

"""
<name>PAMR Predict</name>
<author>Generated using Widget Maker written by Kyle R. Covington</author>
<description>Generates class predictions given an expression matrix.</description>
<tags>PAMR</tags>
"""
from OWRpy import * 
import OWGUI 
import redRGUI, signals
from libraries.base.qtWidgets.tabWidget import tabWidget
from libraries.base.qtWidgets.textEdit import textEdit as redRTextEdit
class pamr_predictMany(OWRpy): 
    settingsList = []
    def __init__(self, **kwargs):
        OWRpy.__init__(self, **kwargs)
        self.setRvariableNames(["pamr.train"])
        self.require_librarys(["pamr"])
        
        """.. rrsignals::"""
        self.inputs.addInput("fit", 'PAMR Data Fit', signals.pamr.RPAMRFit, self.processdata)
        
        """.. rrsignals::"""
        self.inputs.addInput("newData", 'Expression Matrix', signals.base.RMatrix, self.processMatrix)
        #self.outputs.addOutput("pamr.train Output", 'Trained PAMR Model Fit', pamrSignals.RPAMRFit.RPAMRFit)
        
        self.RFunctionParam_matrix = ''
        self.RFunctionParam_data= ''
        
        """.. rrgui::"""
        self.RFunctionParamthreshold_lineEdit =  redRLineEdit(self.controlArea,  label = "Threshold:")
        
        """.. rrgui::"""
        self.outputArea = redRTextEdit(self.controlArea, label = 'PAMR Predictions')
        
        redRCommitButton(self.controlArea, label="Commit", callback = self.commitFunction)
    def processdata(self, data):
        
        if data:
            self.RFunctionParam_data=str(data.getData())
        else:
            self.RFunctionParam_data= ''
    def processMatrix(self, data):
        
        if data:
            self.RFunctionParam_matrix=str(data.getData())
        else:
            self.RFunctionParam_matrix = ''
    def commitFunction(self):
        if unicode(self.RFunctionParam_data) == '': return
        if unicode(self.RFunctionParam_matrix) == '': return
        if unicode(self.RFunctionParamthreshold_lineEdit.text()) == '': 
            self.status.setText('No Threshold Set')
            return
        injection = []
        
        if unicode(self.RFunctionParamthreshold_lineEdit.text()) != '':
            string = 'threshold='+unicode(self.RFunctionParamthreshold_lineEdit.text())
            injection.append(string)
        inj = ','.join(injection)
        if inj != '':
            inj = ',' + inj
        text = self.R('capture.output(pamr.predictmany(fit='+self.RFunctionParam_data+', newx = '+self.RFunctionParam_matrix+inj+'))', wantType = 'list')
        self.outputArea.clear()
        self.outputArea.insertPlainText('\n'.join(text))
        